//
//  TF_LivingServicePage1ViewCellTableViewCell.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/2/21.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TF_LivingServicePage1ViewCell: UITableViewCell {
    
    @objc public let view = UIView()
    @objc public var viewWidth:CGFloat = 0
    @objc public var viewHeight:CGFloat = 0

    //未读信息数
    @objc public var unread:NSInteger = 0
    //最后一张聊天内容
    @objc public var typeMsg = ""
    //最后收到聊天数据的时间
    @objc public var timestamp:NSString = "0"
    @objc public let avatar = UIImageView()
    @objc public let name = UILabel()
    //未读数
    @objc public let unreadText = UILabel()
    @objc public let msg = UILabel()
    @objc public let timeLabel = UILabel()

    @objc public var model:TFMember? {
        didSet {
            if ((model) != nil)
            {
                
                let url:URL? = URL.init(string:model!.avatar)
                self.avatar.sd_setImage(with: url, completed: nil)
       
                var nameY:CGFloat  = 18
                //未读数量等于0
                if (self.unread == 0) {
                    nameY = (self.viewHeight - 10 - 22)/2 ;
                    self.unreadText.isHidden = true
                    self.msg.isHidden = true
                    self.timeLabel.isHidden = true;
                }else{
                    nameY = 18 ;
                    self.unreadText.isHidden = false;
                    self.msg.isHidden = false
                    self.timeLabel.isHidden = false
                }
                self.name.frame = CGRect(x: 72, y: nameY, width: self.viewWidth - 35 - 12, height: 22)
                
                self.name.textColor = UIColor(red: 20/255.0, green: 23/255.0, blue: 26/255.0, alpha: 1.0)
                self.name.text = model!.nickname
                self.name.font = UIFont.PingFang(size: 16)
                
                
                self.unreadText.font = UIFont.PingFang(size: 11)
                self.unreadText.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
                self.unreadText.text = String(self.unread)

                let unreadTextText = self.unreadText.text! as NSString
                let attrsSize: CGSize = unreadTextText.size(withAttributes: [NSAttributedString.Key.font: UIFont(name: "PingFang SC", size: 11) as Any])
                
               
                

               var  unreadTextW:CGFloat  = attrsSize.width + 10
                
                if (unreadTextW < 16) {
                    unreadTextW = 16;
                }
                
               let unreadTextX:CGFloat  = 46 + (unreadTextW/2)
                
                self.unreadText.frame = CGRect(x: unreadTextX, y: 15, width: unreadTextW, height: 16)
                
           

                self.unreadText.layer.cornerRadius = 8;
                self.unreadText.clipsToBounds = true

                self.msg.text = self.typeMsg;
                self.msg.textColor = UIColor(red: 157/255, green: 162/255.0, blue: 167/255.0, alpha: 1.0)
                self.msg.font = UIFont.PingFang(size: 13)
                
                
                self.msg.frame = CGRect(x: 72, y: 45, width: self.viewWidth - 72 - 22, height: 18)
                
      
            //-----------------------时间-------------

                let currentDateStr:NSString = TalkfunSharedTools.getTimestamp(mStr: self.timestamp)

                self.timeLabel.font = UIFont.PingFang(size: 13)
                self.timeLabel.textColor = UIColor(red: 157/255.0, green: 162/255.0, blue: 167/255.0, alpha: 1.0)
                self.timeLabel.text = currentDateStr as String
            }
            
        }

    }
  
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
     
     
        self.contentView.addSubview(self.view)
        self.view.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
        

        self.view.addSubview(self.avatar)

        self.name.numberOfLines = 0;
        self.view.addSubview(self.name)
     
        //未读数

        self.unreadText.backgroundColor = UIColor(red: 246/255.0, green: 85/255.0, blue: 85/255.0, alpha: 1.0)
        
        self.view.addSubview(self.unreadText)
         self.unreadText.isHidden = true
        self.unreadText.textAlignment = .center
        
        //内容

        self.msg.numberOfLines = 0;
        self.view.addSubview(self.msg)

        self.timeLabel.numberOfLines = 0;
        self.view.addSubview(self.timeLabel)
    }
    override func layoutSubviews() {
        super.layoutSubviews()
      
        
        self.view.frame = CGRect(x: 12, y: 0, width: self.frame.size.width - 12 - 12, height: self.frame.size.height - 10)
        self.view.layer.cornerRadius = 4
        
        self.avatar.frame = CGRect(x: 12, y: (self.view.frame.size.height - 50)/2, width: 50, height: 50)
        

        self.avatar.layer.cornerRadius = self.avatar.bounds.size.width / 2;
        self.timeLabel.frame = CGRect(x: self.view.frame.size.width - 35 - 12, y: 20, width: 35, height: 18)
        
    
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
